/**
 * 
 */
package com.doone.telmarket.infrastructure.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.doone.telmarket.domain.OrderSort;
import com.doone.telmarket.domain.ServicePageView;
import com.doone.telmarket.domain.ServiceProduct;
import com.doone.telmarket.domain.ServiceProductView;
import com.doone.telmarket.infrastructure.dao.BaseDaoSupport;
import com.doone.telmarket.infrastructure.dao.ServiceProductDAO;
import com.doone.telmarket.vo.HotBusiVo;
import com.doone.telmarket.vo.ServiceVo;

/**
 * @author Administrator
 *
 */

@Repository("serviceProductDAO")
public class ServiceProductDAOImpl extends BaseDaoSupport implements ServiceProductDAO {

	public ServiceProduct selectServiceProductByPrimaryKey(String productId) {
		ServiceProduct product = (ServiceProduct) getSqlMapClientTemplate().queryForObject(
				"ServiceProduct.selectServiceProductByPrimaryKey", productId);
		return product;
	}

	public int deleteServiceProductByPrimaryKey(String productId) {
		return getSqlMapClientTemplate().delete("ServiceProduct.deleteServiceProductByPrimaryKey", productId);
	}

	public int deleteServiceProductByServiceCode(Integer serviceCode) {
		return getSqlMapClientTemplate().delete("ServiceProduct.deleteServiceProductByServiceCode", serviceCode);
	}

	public ServiceProduct saveServiceProduct(ServiceProduct serviceProduct) {
		return (ServiceProduct)getSqlMapClientTemplate().insert("ServiceProduct.saveServiceProduct", serviceProduct);
	}
	
	@SuppressWarnings("unchecked")
	public List<ServiceProduct> selectServiceProductByPrimaryKeyArray(String productIdArray) {
			return getSqlMapClientTemplate().queryForList("ServiceProduct.selectServiceProductByPrimaryKeyArray",productIdArray);
	}

	@SuppressWarnings("unchecked")
	public List<OrderSort> selectServiceOrderSort() {
		return getSqlMapClientTemplate().queryForList("ServiceProduct.selectOrderSort");
	}

	public int selectBuyCount(String  id) {
		return (Integer)getSqlMapClientTemplate().queryForObject("ServiceProduct.selectBuyCount",id);
	}

	public int selectScCount(int id) {
		return (Integer)getSqlMapClientTemplate().queryForObject("ServiceProduct.selectScCount",id);
	}
	
	@SuppressWarnings("unchecked")
	public List<ServiceProductView> getNextPage(int page,String serviceTypeName){
		int totalCount = (Integer)getSqlMapClientTemplate().queryForObject("ServiceProduct.selectServiceProductCount", serviceTypeName);
		ServicePageView spv = new ServicePageView(serviceTypeName, 10, totalCount, page);
		spv.getNext(page);
		//System.out.println(spv.getMinRowNum());
		//System.out.println(spv.getMaxRowNum());
		//System.out.println(spv.getServiceTypeName());
		//System.out.println(page +" "+serviceTypeName);
		List<ServiceProductView> list = getSqlMapClientTemplate().queryForList("ServiceProduct.selectPageServiceProduct", spv);
		//System.out.println(list.size());
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<ServiceProductView> getPriviPage(int page,String serviceTypeName){
		int totalCount = (Integer)getSqlMapClientTemplate().queryForObject("ServiceProduct.selectServiceProductCount", serviceTypeName);
		ServicePageView spv = new ServicePageView(serviceTypeName, 10, totalCount, page);
		spv.getPrivi(page);
		List<ServiceProductView> list = getSqlMapClientTemplate().queryForList("ServiceProduct.selectPageServiceProduct", spv);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<ServiceProductView> getEndPage(String serviceTypeName) {
		int totalCount = (Integer)getSqlMapClientTemplate().queryForObject("ServiceProduct.selectServiceProductCount", serviceTypeName);
		ServicePageView spv = new ServicePageView(serviceTypeName, 10, totalCount);
		spv.getEndPage();
		List<ServiceProductView> list = getSqlMapClientTemplate().queryForList("ServiceProduct.selectPageServiceProduct", spv);
		return list;
	}
	
	public int getMaxPage(String serviceTypeName){
		int totalCount = (Integer)getSqlMapClientTemplate().queryForObject("ServiceProduct.selectServiceProductCount", serviceTypeName);
		int maxPage = 0;
		if(totalCount%10!=0){
			maxPage = totalCount/10+1;
		}else{
			maxPage = totalCount/10;
		}
		return maxPage;
	}

	@SuppressWarnings("unchecked")
	public List<ServiceProduct> getProductByType(String typeName) {
		
		return getSqlMapClientTemplate().queryForList("ServiceProduct.selectProductByServiceType", typeName);
	}

	public void deleteHotBusiByTypeName(String typeName) {
		getSqlMapClientTemplate().delete("ServiceProduct.deleteHotBusiByTypeName", typeName);
	}

	public void saveHotBusi(ServiceProductView productView) {
		getSqlMapClientTemplate().insert("ServiceProduct.insertHotBusi", productView);
	}

	@SuppressWarnings("unchecked")
	public List<ServiceProductView> selectIndexHotBusi(String serviceTypeName) {
		List<ServiceProductView> list = getSqlMapClientTemplate().queryForList("ServiceProduct.selectIndexHotBusi", serviceTypeName);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<ServiceProductView> getProductView(ServiceVo sv) {
		List<ServiceProductView> list = getSqlMapClientTemplate().queryForList("ServiceProduct.selectProductByTypeNameAndClassifyName", sv);
		return list;
	}

	public void deleteIndexHotByServiceCode(int id) {
		
		getSqlMapClientTemplate().delete("ServiceProduct.deleteIndexHotByServiceCode", id);
	}

	public void deleteHotBusi(String type) {
		getSqlMapClientTemplate().delete("ServiceProduct.deleteHeadHotBusi", type);
	}

	@SuppressWarnings("unchecked")
	public List<HotBusiVo> getHotBusi(String type) {
		List<HotBusiVo> list = getSqlMapClientTemplate().queryForList("ServiceProduct.selectHeadHotBusi", type);
		return list;
	}

	public void insertHotBusi(HotBusiVo view) {
		
		System.out.println(view.getServiceCode());
		System.out.println(view.getType());
		System.out.println(view.getOrderId());
		getSqlMapClientTemplate().insert("ServiceProduct.insertHeadHotBusi", view);
	}

}
